<?php

namespace Admin\Controller;

use Core\Constant;
use Core\Controller\AdminBaseController;
use Core\Exception\BusinessException;
use Core\Helper\ApplicationDirectoryHelper;
use Core\Session\AdminSessionHelper;
use Exception;
use Model\Page;
use Zend\Json\Json;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;

class MenuController extends AdminBaseController {

    public function indexAction(){

        $menuTable = $this->getTableFinder()->getMenuTable(
            AdminSessionHelper::getCurrentLang()
        );
        $allMenu = $menuTable->getAll();

        $pageTable = $this->getTableFinder()->getPageTable();
        $allActivePage = $pageTable->getAllActivePage();
        return new ViewModel(array('allMenu'=>$allMenu,
            'allActivePage'=>$allActivePage));

    }

    public function getMenuContentAction(){
        $menuId = $this->params()->fromPost('menuId');
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        $menuItem = $this->getTableFinder()->getMenuTable(
            AdminSessionHelper::getCurrentLang()
        )->getMenu($menuId);
        $viewModel->setVariable('menuItem',$menuItem);
        return $viewModel;
    }


    public function getMenuItemParentAction(){
        try{

            $id = $this->params()->fromPost('id');

            if(!$id){
                throw new BusinessException("Chưa truyền id!");
            }
            $menuItemTable = $this->getTableFinder()->getMenuItemTable();

            $menuItem_Item = $menuItemTable->getMenuItem($id);
            $directParent = $menuItem_Item->getDirectParent();

            if(!$directParent)
                return new JsonModel(array('success'=>true, "upable"=>false, "downable"=>false));
            else{
                $upable = true;
                $downable = true;

                if($directParent->lft == $menuItem_Item->lft - 1 )
                    $upable = false;
                if($directParent->rgt == $menuItem_Item->rgt + 1 )
                    $downable = false;
                return new JsonModel(array('success'=>true, "upable"=>$upable, "downable"=>$downable));
            }
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function upMenuItemAction(){
        try{
            $id = $this->params()->fromPost('id');

            if(!$id){
                throw new BusinessException("Chưa truyền id!");
            }
            $menuItemTable = $this->getTableFinder()->getMenuItemTable();
            $menu_Item = $menuItemTable->getMenuItem($id);
            $menu_Item->upMyPosition();

            return new JsonModel(array('success'=>true));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function downMenuItemAction(){
        try{
            $id = $this->params()->fromPost('id');

            if(!$id){
                throw new BusinessException("Chưa truyền id!");
            }
            $menuItemTable = $this->getTableFinder()->getMenuItemTable();
            $menu_Item = $menuItemTable->getMenuItem($id);
            $menu_Item->downMyPosition();

            return new JsonModel(array('success'=>true));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function removeMenuItemAction(){
        try{
            $id = $this->params()->fromPost('id');

            if(!$id){
                throw new BusinessException("Chưa truyền id!");
            }
            $menuItemTable = $this->getTableFinder()->getMenuItemTable();
            $menuItemTable->removeMenuItem($id);
            return new JsonModel(array('success'=>true));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function saveAction(){
        try{

            $id = $this->params()->fromPost('id');
            $code = $this->params()->fromPost('code');
            $name = $this->params()->fromPost('name');
            $description = $this->params()->fromPost('description');
            $menuTable = $this->getTableFinder()->getMenuTable(AdminSessionHelper::getCurrentLang());
            $menuTable->update(array( "code"=>$code,"name"=>$name,"description"=>$description), $id);
            return new JsonModel(array('success'=>true));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }


    public function saveMenuItemAction(){
        try{
            $id = $this->params()->fromPost('id');

            $label = $this->params()->fromPost('name');
            $url = $this->params()->fromPost('url');
            $parentId = $this->params()->fromPost('parentId');
            $target = $this->params()->fromPost('target');

            $menuItemTable = $this->getTableFinder()->getMenuItemTable();
            $parentMenuItem = $menuItemTable->getMenuItem($parentId);

            if($id){
                $data = array("label"=>$label,"url"=>$url,'target'=>$target,
                    "menu_id"=>$parentMenuItem->menu_id);
                $menuItemTable->updateMenuItem($data,$id);
            }else{
                $data = array( "label"=>$label,"url"=>$url,'target'=>$target,
                    "menu_id"=>$parentMenuItem->menu_id, "parentId"=>$parentId);
                $menuItemTable->addMenuItem($data);
            }
            return new JsonModel(array('success'=>true));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }


    public  function getParameterBoxAction(){
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        $url = $this->params()->fromPost('url');
        $pageTable = $this->getTableFinder()->getPageTable();
        $pageItem = $pageTable->getPageByRoute($url);
        if(!$pageItem)
            $viewModel->setVariable('error','Không tìm thấy page hợp lệ. Vui lòng chọn page khác.');
        else
            $viewModel->setVariable('pageItem',$pageItem);
        return $viewModel;
    }
}